/*
 * RSBoolArray.cpp
 *
 *  Created on: 2010/03/17
 *      Author: alexandre
 */

#include "RSBoolArray.h"
#include "RSBool.h"
#include "RSSerializer.h"

namespace Realsphere {

    RSBoolArray::RSBoolArray () :
            RSArrayElement ()
    {
        SetTag( RS_BOOL_ARRAY_TAG, false );
    }

    RSBoolArray::RSBoolArray ( const QString& rId ) :
            RSArrayElement ( rId )
    {
        SetTag( RS_BOOL_ARRAY_TAG, false );
    }

    RSBoolArray::RSBoolArray ( const QString& rId, const QString& rName ) :
            RSArrayElement ( rId, rName )
    {
        SetTag( RS_BOOL_ARRAY_TAG, false );
    }

    RSBoolArray::RSBoolArray ( const bool rArray[] ) :
            RSArrayElement ()
    {
        SetTag( RS_BOOL_ARRAY_TAG, false );
        Append ( rArray, false );
    }

    RSBoolArray::RSBoolArray ( const bool rArray[], const QString& rId ) :
        RSArrayElement ( rId )
    {
        SetTag( RS_BOOL_ARRAY_TAG, false );
        Append ( rArray, false );
    }

    RSBoolArray::RSBoolArray ( const bool rArray[], const QString& rId, const QString& rName ) :
        RSArrayElement ( rId, rName )
    {
        SetTag( RS_BOOL_ARRAY_TAG, false );
        Append ( rArray, false );
    }

    RSBoolArray::RSBoolArray ( const bool* rArray, unsigned int rSize ) :
            RSArrayElement ()
    {
        SetTag( RS_BOOL_ARRAY_TAG, false );
        Append ( rArray, rSize, false );
    }

    RSBoolArray::RSBoolArray ( const bool* rArray, unsigned int rSize , const QString& rId ) :
        RSArrayElement ( rId )
    {
        SetTag( RS_BOOL_ARRAY_TAG, false );
        Append ( rArray, rSize, false );
    }

    RSBoolArray::RSBoolArray ( const bool* rArray, unsigned int rSize , const QString& rId, const QString& rName ) :
        RSArrayElement ( rId, rName )
    {
        SetTag( RS_BOOL_ARRAY_TAG, false );
        Append ( rArray, rSize, false );
    }

    RSBoolArray::~RSBoolArray ()
    {
        mData.clear();
    }

    RSArrayElementType RSBoolArray::ArrayType () const
    {
        return RS_BOOL_ARRAY;
    }

    int RSBoolArray::Count () const
    {
        return mData.size();
    }

    RSDataPointer RSBoolArray::DataPointer ()
    {
        return mData.data();
    }

    RSDataSize RSBoolArray::SizeOf () const
    {
        return mData.size() * sizeof(bool);
    }

    QString RSBoolArray::ToString () const
    {
        return RSSerializer::serialize( mData );
    }

    void RSBoolArray::Clear( bool rEmit )
    {
        mData.clear();
        if ( rEmit )
            emit DataCleared();
    }

    void RSBoolArray::Append ( const RSValueElement& rValue, bool rEmit )
    {
        if ( rValue.ValueType() == RS_BOOL  )
        {
            const RSBool* value = RSConstBoolCast(&rValue);
            Append ( *value, rEmit );
        }
    }

    void RSBoolArray::Append ( const RSBool& rValue, bool rEmit )
    {
        mData.append( rValue.Value() );
        if ( rEmit )
            emit DataAdded ( rValue );
    }

    void RSBoolArray::Append ( const bool& rValue, bool rEmit )
    {
        RSBool value( rValue );
        Append ( rValue, rEmit );
    }

    void RSBoolArray::Append ( const bool rValue[], bool rEmit )
    {
        for ( unsigned int i = 0; i < sizeof ( rValue ); i++ )
            Append ( rValue[i], false );

        if ( rEmit )
            emit DataAdded ( sizeof ( rValue ) );
    }

    void RSBoolArray::Append ( const bool* rValue, unsigned int rSize, bool rEmit )
    {
        for ( unsigned int i = 0; i < rSize; i++, rValue++ )
            Append ( *rValue, false );

        if ( rEmit )
            emit DataAdded ( rSize );
    }

    void RSBoolArray::Remove ( unsigned int rIndex, bool rEmit )
    {
        mData.remove( rIndex );
        if ( rEmit )
            emit DataRemoved ( rIndex );
    }

    void RSBoolArray::Remove ( unsigned int rIndex, unsigned int rSize, bool rEmit )
    {
        mData.remove ( rIndex, rSize );
        if ( rEmit )
            emit DataRemoved ( rIndex, rSize );
    }

    void RSBoolArray::SetValue ( unsigned int rIndex, const RSValueElement& rValue, bool rEmit )
    {
        if ( rValue.ValueType() == RS_BOOL ) {
            const RSBool* value = RSConstBoolCast(&rValue);
            SetValue ( rIndex, *value, rEmit );
        }
    }

    void RSBoolArray::SetValue ( unsigned int rIndex, const RSBool& rValue, bool rEmit )
    {
        mData[rIndex] = rValue.Value();
        if ( rEmit )
            emit DataChanged ( rIndex, rValue );
    }

    void RSBoolArray::SetValue ( unsigned int rIndex, const bool& rValue, bool rEmit )
    {
        RSBool value ( rValue );
        SetValue ( rIndex, value, rEmit );
    }

    bool& RSBoolArray::operator[] ( int rIndex )
    {
        return mData[rIndex];
    }

    const bool& RSBoolArray::operator[] ( int rIndex ) const
    {
        return mData[rIndex];
    }
}
